<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['financial:financial:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:financial:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:financial:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['financial:financial:export']"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="financialList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="用户编码" align="center" prop="usercode" />
      <el-table-column label="名称" align="center" prop="username" />
      <el-table-column label="性别" align="center" prop="usersex" />
      <el-table-column label="部门" align="center" prop="userdep" />
      <el-table-column label="职务" align="center" prop="userrole" />
      <el-table-column label="手机" align="center" prop="usermobile" />
      <el-table-column label="身份证号" align="center" prop="useridCard" />
      <el-table-column label="邮箱" align="center" prop="useremail" />
      <el-table-column label="出生日期" align="center" prop="birthday" />
      <el-table-column label="入职日期" align="center" prop="entertime" />
      <el-table-column label="离职日期" align="center" prop="leavetime" />
      <el-table-column label="银行名称" align="center" prop="bankname" />
      <el-table-column label="银行账号" align="center" prop="bankaccount" />
      <el-table-column label="学历" align="center" prop="degree" />
      <el-table-column label="在职状态" align="center" prop="userstate" />
      <el-table-column label="计提科目" align="center" prop="accrualsubject" />
      <el-table-column label="所属账套" align="center" prop="accountSetsId" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['financial:financial:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['financial:financial:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <!-- <el-dialog
      :title="upload.title"
      v-model="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls"
        :auto-upload="false"
        :disabled="upload.isUploading"
        :on-change="handleFileChange"
        :on-remove="handleFileRemove"
        drag
      >
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <span>仅允许导入xlsx、xls格式文件。</span>
            <el-link
              type="primary"
              :underline="false"
              style="font-size: 12px; vertical-align: baseline"
              @click="importTemplate"
              >下载模板</el-link
            >
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitFileForm"
            :loading="upload.isUploading"
            >确 定</el-button
          >
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog> -->

    <!-- 添加或修改职员表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="financialRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="用户编码" prop="usercode">
          <el-input v-model="form.usercode" placeholder="请输入用户编码" />
        </el-form-item>
        <el-form-item label="名称" prop="username">
          <el-input v-model="form.username" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="部门" prop="userdep">
          <el-input v-model="form.userdep" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="职务" prop="userrole">
          <el-input v-model="form.userrole" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="手机" prop="usermobile">
          <el-input v-model="form.usermobile" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="身份证号" prop="useridCard">
          <el-input v-model="form.useridCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="useremail">
          <el-input v-model="form.useremail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入出生日期" />
        </el-form-item>
        <el-form-item label="入职日期" prop="entertime">
          <el-input v-model="form.entertime" placeholder="请输入入职日期" />
        </el-form-item>
        <el-form-item label="离职日期" prop="leavetime">
          <el-input v-model="form.leavetime" placeholder="请输入离职日期" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankname">
          <el-input v-model="form.bankname" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankaccount">
          <el-input v-model="form.bankaccount" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="学历" prop="degree">
          <el-input v-model="form.degree" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="在职状态" prop="userstate">
          <el-input v-model="form.userstate" placeholder="请输入在职状态" />
        </el-form-item>
        <el-form-item label="计提科目" prop="accrualsubject">
          <el-input
            v-model="form.accrualsubject"
            placeholder="请输入计提科目"
          />
        </el-form-item>
        <el-form-item label="所属账套" prop="accountSetsId">
          <el-input v-model="form.accountSetsId" placeholder="请输入所属账套" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Financial">
import {
  listFinancial,
  getFinancial,
  delFinancial,
  addFinancial,
  updateFinancial,
  importSocialsecurity,
  getImportTemplate,
} from "@/api/financial/financial";
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();

const financialList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    username: null,
  },
  rules: {
    usercode: [
      { required: true, message: "用户编码不能为空", trigger: "blur" },
    ],
    username: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    accountSetsId: [
      { required: true, message: "所属账套不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询职员表列表 */
function getList() {
  loading.value = true;
  listFinancial(queryParams.value).then((response) => {
    financialList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    usercode: null,
    username: null,
    usersex: null,
    userdep: null,
    userrole: null,
    usermobile: null,
    useridCard: null,
    useremail: null,
    birthday: null,
    entertime: null,
    leavetime: null,
    bankname: null,
    bankaccount: null,
    degree: null,
    userstate: null,
    accrualsubject: null,
    accountSetsId: null,
    note: null,
  };
  proxy.resetForm("financialRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加职员表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getFinancial(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改职员表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["financialRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateFinancial(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFinancial(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除职员表编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delFinancial(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "financial/financial/export",
    {
      ...queryParams.value,
    },
    `financial_${new Date().getTime()}.xlsx`
  );
}

// 导入参数
const upload = reactive({
  open: false,
  title: "",
  isUploading: false,
  fileList: [], // 添加文件列表存储
});

// 文件上传ref
const uploadRef = ref(null);

/** 导入按钮操作 */
// function handleImport() {
//   upload.title = "五险一金数据导入";
//   upload.open = true;
// }

/** 下载模板操作 */
function importTemplate() {
  getImportTemplate().then((res) => {
    const blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
    const link = document.createElement("a");
    link.href = window.URL.createObjectURL(blob);
    link.download = "五险一金导入模板.xlsx";
    link.click();
    window.URL.revokeObjectURL(link.href);
  });
}

// 文件状态改变
function handleFileChange(file) {
  upload.fileList = [file];
}

// 文件移除
function handleFileRemove() {
  upload.fileList = [];
}

// 提交上传文件
function submitFileForm() {
  if (!upload.fileList.length) {
    proxy.$modal.msgError("请选择要上传的文件");
    return;
  }
  const formData = new FormData();
  formData.append("file", upload.fileList[0].raw);
  upload.isUploading = true;
  importSocialsecurity(formData)
    .then((res) => {
      if (res.code === 200) {
        proxy.$modal.msgSuccess(res.msg);
        upload.open = false;
        getList();
      } else {
        proxy.$modal.msgError(res.msg);
      }
      upload.isUploading = false;
      upload.fileList = [];
      if (uploadRef.value) {
        uploadRef.value.clearFiles();
      }
    })
    .catch(() => {
      upload.isUploading = false;
    });
}

getList();
</script>

<style scoped>
.upload-demo {
  display: inline-block;
}
</style>
